package com.waterworld.watch.mine.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.BounceInterpolator;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuCreator;
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuListView;
import com.waterworld.watch.R;
import com.waterworld.watch.common.activity.BaseActivity;
import com.waterworld.watch.common.application.MyApplication;
import com.waterworld.watch.common.bean.NotificationBean;
import com.waterworld.watch.common.bean.NotificationCategoryBean;
import com.waterworld.watch.common.bean.NotificationDetailBean;
import com.waterworld.watch.common.customview.customRecycleView.SlidingItemMenuRecyclerView;
import com.waterworld.watch.common.db.DbController;
import com.waterworld.watch.common.db.Notification;
import com.waterworld.watch.common.net.BaseObserverListener;
import com.waterworld.watch.common.net.BaseResultBean;
import com.waterworld.watch.common.net.NetManager;
import com.waterworld.watch.common.util.ScreenAdapterUtil;
import com.waterworld.watch.common.util.TimeUtils;
import com.waterworld.watch.mine.adapter.NotificationAdapter;
import com.waterworld.watch.mine.interfaces.IMineManager;
import com.waterworld.watch.mine.manager.MineManager;
import com.waterworld.watch.push.contant.MessageContants;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Subscriber;

public class NotificationActivity extends BaseActivity {

    @BindView(R.id.header_parent)
    LinearLayout header_parent;

    @BindView(R.id.header_back)
    ImageView header_back;

    @BindView(R.id.header_title)
    TextView header_title;

    @BindView(R.id.header_menu)
    ImageButton header_menu;

    @BindView(R.id.rv_item_menu)
    RecyclerView rv_item_menu;

    private List<Notification> mList = new ArrayList<>();
    ;
    private NotificationAdapter mAdapter;
    private IMineManager iMineManager = MineManager.getInstance();

    private DbController dbController = DbController.getInstance(MyApplication.getContext());
    private NotificationCategoryBean bean;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mine_message_notification);
        ButterKnife.bind(this);
        bean = (NotificationCategoryBean) getIntent().getSerializableExtra("bean");
        initView();
        initData();
    }


    private void initView() {
        setViewSize(header_parent, ViewGroup.LayoutParams.MATCH_PARENT, ScreenAdapterUtil.getHeightPx(this) / 10);
        header_title.setText(bean.getGroupName());
        header_back.setVisibility(View.VISIBLE);
        header_title.setVisibility(View.VISIBLE);
        header_menu.setVisibility(View.VISIBLE);
    }

    private void initData() {
        NetManager.getInstance().listMessage(1,100,bean.getMsgId(),bean.getGroupId(),new Subscriber<BaseResultBean<NotificationDetailBean[]>>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(BaseResultBean<NotificationDetailBean[]> baseResultBean) {
                if (baseResultBean.getCode() == 0) {
                    if (baseResultBean.getData() != null) {
                        for (NotificationDetailBean bean : baseResultBean.getData()) {
                            Notification notification = new Notification(null,bean.getMsgId(),bean.getGroupId(),bean.getWatch_id(),bean.getWatchUserName(),bean.getWatchUserPhone(),bean.getContent(),bean.getType(),bean.getCreateTime(),0);
                            dbController.isNeedtoInsert(notification);
                        }
                    }

                    mList.addAll(dbController.getNotification(bean.getGroupId()));
                    mAdapter = new NotificationAdapter(NotificationActivity.this, mList);
                    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(NotificationActivity.this);
                    linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                    rv_item_menu.setLayoutManager(linearLayoutManager);
                    rv_item_menu.setAdapter(mAdapter);

                }
            }
        });
    }

    @OnClick(R.id.header_back)
    void onBack() {
        finish();
    }
}
